package com.anche.cywebnew.Utils.HikUtil;

import com.anche.cywebnew.DTO.HikBaseDTO;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 布防报警
 */
public class AlarmUtil extends CommonUtil {

    private static final Logger logger = LoggerFactory.getLogger(CaptureUtil.class);

    HCNetSDK.FMSGCallBack fMSFCallBack;//报警回调函数实现

    NativeLong lAlarmHandle;

    public AlarmUtil(HCNetSDK.FMSGCallBack fMSFCallBack) {
        this.fMSFCallBack = fMSFCallBack;
    }

    public boolean doInitListener() {
        boolean isSuccess = false;
        if (doInit()) {
            setConnectTime(2000, 1);
            setReconnect(10000, true);
            logger.info("开始设置布防回调参数");
            if (fMSFCallBack != null) {
                Pointer pUser = null;
                if (!hCNetSDK.NET_DVR_SetDVRMessageCallBack_V30(fMSFCallBack, pUser)) {
                    logger.info("设置布防回调函数失败!");
                }else {
                    isSuccess = true;
                    logger.info("设置布防回调函数成功!");
                }
            } else {
                logger.info("布防回调参数传入为null");
            }
        }
        return isSuccess;
    }

    public NativeLong setUpAlarm(NativeLong lUserID) {
        NativeLong lAlarmHandle = hCNetSDK.NET_DVR_SetupAlarmChan_V30(lUserID);
        if (lAlarmHandle.longValue() != -1) {
            logger.info("设备布防成功");
        } else {
            int code = hCNetSDK.NET_DVR_GetLastError();
            String msg = HikErrorUtil.getMsgByCode(code);
            logger.info("设备布防失败，code：" + code + "，msg：" + msg);
        }
        return lAlarmHandle;
    }

    public Map<String, Object> setAlarmConfig(HikBaseDTO hikBaseDTO, NativeLong ch, HCNetSDK.NET_DVR_PICCFG_V30 dvr_piccfg_v30) {
        Map<String, Object> map = new HashMap<>();
        if (doInit()) {
            NativeLong lUserID = doLogin(hikBaseDTO);
            if (lUserID.longValue() != -1) {
                dvr_piccfg_v30.write();
                boolean isConfig = hCNetSDK.NET_DVR_SetDVRConfig(
                        lUserID,
                        HCNetSDK.NET_DVR_SET_PICCFG_V30,
                        ch,
                        dvr_piccfg_v30.getPointer(),
                        dvr_piccfg_v30.size()
                );
                dvr_piccfg_v30.read();
                if (isConfig) {
                    logger.info("配置成功");
                    map.put("code", "0");
                    map.put("msg", lUserID);
                } else {
                    int code = hCNetSDK.NET_DVR_GetLastError();
                    String msg = HikErrorUtil.getMsgByCode(code);
                    logger.info("配置失败，code：" + code + "，msg：" + msg);
                    map.put("code", "0");
                    map.put("msg", "配置失败");
                }
            } else {
                map.put("code", "0");
                map.put("msg", "登陆失败");
            }
        } else {
            map.put("code", "0");
            map.put("msg", "初始化失败");
        }
        return map;
    }

    public NativeLong doListenAlarm(NativeLong lUserID) {
        NativeLong lAlarmHandle = new NativeLong(-1);
        logger.info("开始设置布防回调参数");
        if (fMSFCallBack != null) {
            Pointer pUser = null;
            if (!hCNetSDK.NET_DVR_SetDVRMessageCallBack_V30(fMSFCallBack, pUser)) {
                logger.info("设置布防回调函数失败!");
            }
            lAlarmHandle = hCNetSDK.NET_DVR_SetupAlarmChan_V30(lUserID);
            if (lAlarmHandle.intValue() == -1) {
                logger.info("布防失败");
            } else {
                logger.info("布防成功");
            }
        } else {
            logger.info("布防回调参数传入为null");
        }
        return lAlarmHandle;
    }

    public boolean doListenAlarm(HikBaseDTO hikBaseDTO) {
        boolean isSuccess = false;
        lAlarmHandle = new NativeLong(-1);
        if (doInit()) {
            lUserID = doLogin(hikBaseDTO);
            if (lUserID.longValue() != -1) {
                logger.info("开始设置布防回调参数");
                if (fMSFCallBack != null) {
                    Pointer pUser = null;
                    if (!hCNetSDK.NET_DVR_SetDVRMessageCallBack_V30(fMSFCallBack, pUser)) {
                        logger.info("设置布防回调函数失败!");
                    }
                    lAlarmHandle = hCNetSDK.NET_DVR_SetupAlarmChan_V30(lUserID);
                    if (lAlarmHandle.intValue() == -1) {
                        logger.info("布防失败");
                    } else {
                        isSuccess = true;
                        logger.info("布防成功");
                    }
                } else {
                    logger.info("布防回调参数传入为null");
                }
            }
        }
        return isSuccess;
    }

    /**
     * 关闭布防监听
     *
     * @param lListenHandle 布防句柄
     */
    public void doCloseListen(NativeLong lListenHandle) {
        if (hCNetSDK.NET_DVR_CloseAlarmChan_V30(lListenHandle)) {
            logger.info("停止布防成功");
        } else {
            int code = hCNetSDK.NET_DVR_GetLastError();
            String msg = HikErrorUtil.getMsgByCode(code);
            errMsg = MessageFormat.format(errMsg, "停止布防失败", code, msg);
            logger.info(errMsg);
        }
    }

    /**
     * 关闭布防监听
     */
    public void doCloseListen() {
        if (hCNetSDK.NET_DVR_CloseAlarmChan_V30(lAlarmHandle)) {
            logger.info("停止布防成功");
        } else {
            int code = hCNetSDK.NET_DVR_GetLastError();
            String msg = HikErrorUtil.getMsgByCode(code);
            errMsg = MessageFormat.format(errMsg, "停止布防失败", code, msg);
            logger.info(errMsg);
        }
        doLogout();
        doDestroy();
    }
}

